<template>
    <div class="siginIn">
        <b-modal class="b-modal" ref="b-modal-signin" title="Weblog登录">
            <b-form>
                <b-form-group
                    label="手机号/邮箱："
                    label-for="userName"
                >
                    <b-form-input
                        id="userName"
                        v-model="signInForm.userName"
                    >
                        </b-form-input>
                </b-form-group>
                <b-form-group
                    label="密码："
                    label-for="password"
                >
                    <b-form-input 
                        id="password" 
                        type="password"
                        v-model="signInForm.password"
                    >
                    </b-form-input>
                </b-form-group>
                <b-form-invalid-feedback :state="signInResult">
                    请输入正确的账号或密码
                </b-form-invalid-feedback>
            </b-form>
            <template v-slot:modal-footer>
                <b-button variant="info" @click="signIn()">登录</b-button>
                <b-button @click="$emit('onChange', true)" variant="success">没有账号</b-button>
            </template>
        </b-modal>
    </div>
</template>

<script>

import Axios from 'axios';
import { signIn } from '@/../api/api.js'
import store from '@/store/index.js';

export default {
    data() {
        return {
            signInForm: {
                userName: "",
                password: "",
                testCode: ""
            },
            signInResult: true
        }
    },
    methods: {
        changeSignIn(opt) {
            if(opt == "open") {
                this.$refs['b-modal-signin'].show();
            }
            else if(opt == "close") {
                this.$refs['b-modal-signin'].hide();
            }
        },
        signIn() {
            Axios.post(signIn,{
                userAccount: this.signInForm.userName,
                userPassword: this.signInForm.password
            })
            .then(Response => {
                if (Response.data.message != 'success') {
                    window.alert("账号或密码错误！");
                }
                else {
                    store.commit('updateUser',Response.data.data.user);
                    store.commit('updateIsUser');
                    this.$refs['b-modal-signin'].hide();
                } 
            })
            .catch(err => {
                this.signInResult = false;
                window.alert('登录失败，请稍后再试！');
            })
            .finally()
        }
    }
}
</script>

<style lang="stylus" scoped>

</style>